package com.example.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.example.web.config.JedisUtil;
import com.example.web.model.User;
@RestController
public class TestController {

	private static final String key ="user:id:";

	
	
	@GetMapping("/test1")
	public User test1() {
		
		User u =new User();
		u.setId(1);
		u.setName("小红");
		u.setAge("18");
		String res = JedisUtil.setObject(key, u, 5000);
		System.out.println(res);
		
		return (User) JedisUtil.getObject(key);
	}
	
	@GetMapping("/test2")
	public String test2() {
		User u =new User();
		u.setId(1);
		u.setName("小红");
		u.setAge("18");
		String userJson = JSON.toJSONString(u);
		String res = JedisUtil.setJson(key+1, userJson, 5000);
		System.out.println(res);
		System.out.println(JedisUtil.keysB(key));
		return  JedisUtil.getJson(key+1);
	}
	
	@GetMapping("/test3")
	public Map<String,Object> test3() {
		Map<String,Object> map =new HashMap<String, Object>();
		map.put("as","asd");
		map.put("aaa","asdasd");
		System.out.println(JedisUtil.setMap("map1", map));
		System.out.println(JedisUtil.getMap("map1"));
		return JedisUtil.getMap("map1");
	}
	
	@GetMapping("/test4")
	public Map<String,Object> test4() {
		Map<String,Object> map =new HashMap<String, Object>();
		User u =new User();
		u.setId(1);
		u.setName("小红");
		u.setAge("18");
		User u1 =new User();
		u1.setId(1);
		u1.setName("小绿");
		u1.setAge("18");
		map.put("as",u);
		map.put("aaa",u1);
		JedisUtil.setMap("12", map);
		System.out.println(JedisUtil.getMapKeyEntity("12", "as", User.class));
		
		List<User> list =  new ArrayList<User>();
		list.add(u);
		list.add(u1);
		
		JedisUtil.setMapKeyExpire("255", "a", list, 5000);
		
		System.out.println(JedisUtil.getMapKeyListEntity("255", "a", User.class));
		System.out.println("******"+JedisUtil.getMapKeyListEntity("255", "a", User.class).get(0));
		
		System.out.println(JedisUtil.getMapEntityClass("12", User.class));
		System.out.println(JedisUtil.getMapList("255",User.class));
		
		System.out.println(JedisUtil.delMapKey("12", "as"));
		
		return JedisUtil.getMap("12");
	}
	@GetMapping("/test5")
	public void test5() {
		List<User> list =  new ArrayList<User>();
		User u =new User();
		u.setId(1);
		u.setName("小红");
		u.setAge("18");
		User u1 =new User();
		u1.setId(1);
		u1.setName("小绿");
		u1.setAge("18");
		list.add(u);
		list.add(u1);
		System.out.println(JedisUtil.setList("asd", list));
		System.out.println("---"+JedisUtil.getListEntity("asd", User.class));
		System.out.println("--"+JedisUtil.getListEntity("asd", User.class).get(0).getName());
		System.out.println("*-*-"+JedisUtil.getListString("asd").get(0));
	}
	@GetMapping("/test6")
	public void test6() {
		System.out.println(JedisUtil.incr("aa"));
		System.out.println(JedisUtil.incrBy("a1", 155));
	}
}
